package jp.co.showbiz.admin.report.imp;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import jp.co.showbiz.admin.report.dao.ReportDao;
import jp.co.showbiz.admin.talent.imp.TalentDto;
import jp.co.showbiz.common.base.BaseException;
import jp.co.showbiz.common.entity.CommonMstBean;
import jp.co.showbiz.common.entity.ContentMstBean;
import jp.co.showbiz.common.entity.FileBean;
import jp.co.showbiz.common.entity.KijiBean;
import jp.co.showbiz.common.entity.KijiDigestBean;
import jp.co.showbiz.common.entity.KijiDigestContentBean;
import jp.co.showbiz.common.entity.TalentContentBean;
import jp.co.showbiz.common.entity.TalentSenyouBean;
import jp.co.showbiz.common.util.CommUtil;

import org.xml.sax.SAXException;

import com.opensymphony.xwork2.ActionContext;

public class ReportServiceImpl {
	private ReportDao dao;
	private HashMap<String, String> globeMap;
	public ReportServiceImpl() throws BaseException, IOException {
		dao = new ReportDao();
	}
	public ReportServiceImpl(HashMap<String, String> globeMap) throws BaseException, IOException {
		dao = new ReportDao();
		this.globeMap = globeMap;
	}
	public HashMap<String, Integer> createPageMap(String disc, int curPage) throws Exception {
		int total = dao.getTotalCount(disc);
		return CommUtil.getPageInfo(curPage, total, 10, 10);
	}
	public List<ReportDto> getReportList(String disc, HashMap<String, Integer> pageMap) throws Exception {
		int skip = pageMap.get("skip");
		return dao.getReportList(disc, skip, 10);
	}
	
    public HashMap<String, String> moveReportResource(ReportDto rBean, String kijiId, String disc) throws Exception {
    	HashMap<String, String> fileMap = new HashMap<String, String>();
    	fileMap.put("kijiId", kijiId);
    	String filePath = globeMap.get("globe.path.commonImg");
    	String basicFileName = kijiId.concat("_").concat(rBean.getTalent());
    	
    	//コンテンツ保存ディレクトリ生成
    	String imageFilePathStr = filePath.concat(globeMap.get("globe.path.photo")).concat("/").concat(rBean.getTalent()).concat("/");
    	File imageFilePath = new File(imageFilePathStr);
    	if (!imageFilePath.exists()) {
    		imageFilePath.mkdirs();
    	}
    	String movieFilePathStr = filePath.concat(globeMap.get("globe.path.movie")).concat("/").concat(rBean.getTalent()).concat("/");
    	File movieFilePath = new File(movieFilePathStr);
    	if (!movieFilePath.exists()) {
    		movieFilePath.mkdirs();
    	}
    	//表紙イメージ
    	moveReportCoverImg(rBean, basicFileName, fileMap, imageFilePathStr);
    	//PICKUP
    	movePickupImg(rBean, basicFileName, fileMap, imageFilePathStr);
    	//メインイメージ
    	moveReportMainImg(rBean, basicFileName, fileMap, imageFilePathStr);
    	//動画１
    	moveReportMovie(rBean, basicFileName, fileMap, movieFilePathStr, 1);
    	//動画2
    	moveReportMovie(rBean, basicFileName, fileMap, movieFilePathStr, 2);
    	//動画3
    	moveReportMovie(rBean, basicFileName, fileMap, movieFilePathStr, 3);
        
        return fileMap;
    }

    private boolean movePickupImg(ReportDto rBean, String basicFileName, HashMap<String, String> fileMap, String imageFilePathStr) throws Exception {
    	FileBean pickupVga = rBean.getPickupVga();
    	FileBean pickupQvga = rBean.getPickupQVga();
    	//DB登録用カバーイメージ名
    	String coverImage = "pickup_".concat(basicFileName);

    	//カバーイメージコピー
    	if (pickupVga != null) {
	    	File newFile = new File(imageFilePathStr + "VT_" + coverImage.concat(getExtensionFromFileName(pickupVga.getFileFileName())));
	    	CommUtil.copyFile(pickupVga.getFile(), newFile, true);
    	}
    	if (pickupQvga != null) {
    		File newFile = new File(imageFilePathStr + "QT_" + coverImage.concat(getExtensionFromFileName(pickupQvga.getFileFileName())));
	    	CommUtil.copyFile(pickupQvga.getFile(), newFile, true);
    	}
    	if (pickupVga != null && pickupQvga != null) {
    		fileMap.put("photo_pickup", coverImage.concat(getExtensionFromFileName(pickupVga.getFileFileName())));
    		return true;
    	}
    	return false;
    }
    
	private boolean moveReportCoverImg(ReportDto rBean, String basicFileName, HashMap<String, String> fileMap, String imageFilePathStr) throws Exception {
		//DB登録用カバーイメージ名
		FileBean coverVga = rBean.getCoverVga();
    	FileBean coverQVga = rBean.getCoverQVga();
    	String coverImage = "cover_".concat(basicFileName);
    	if (coverVga != null) {
	    	//カバーイメージコピー
	    	File newFile = new File(imageFilePathStr + "VT_" + coverImage.concat(getExtensionFromFileName(coverVga.getFileFileName())));
	    	CommUtil.copyFile(coverVga.getFile(), newFile, true);
    	}
    	if (coverQVga != null) {
    		File newFile = new File(imageFilePathStr + "QT_" + coverImage.concat(getExtensionFromFileName(coverVga.getFileFileName())));
	    	CommUtil.copyFile(coverQVga.getFile(), newFile, true);
    	}
    	if (coverVga != null && coverQVga != null) {
    		fileMap.put("photo_cover", coverImage.concat(getExtensionFromFileName(coverVga.getFileFileName())));
    		return true;
    	}
    	return false;
	}
	
	private boolean moveReportMainImg(ReportDto rBean, String basicFileName, HashMap<String, String> fileMap, String imageFilePathStr) throws Exception {
		FileBean mainVgaThum = rBean.getMainVgaThum();
    	FileBean mainQVgaThum = rBean.getMainQVgaThum();
    	FileBean mainVgaDetail = rBean.getMainVgaDetail();
    	FileBean mainQVgaDetail = rBean.getMainQVgaDetail();
    	FileBean mainVgaOriginal = rBean.getMainVgaOriginal();
    	FileBean mainVgaOriginal2 = rBean.getMainVgaOriginal2();
    	FileBean mainQVgaOriginal = rBean.getMainQVgaOriginal();
    	FileBean mainQVgaOriginal2 = rBean.getMainQVgaOriginal2();
    	String mainImage = "main_".concat(basicFileName);
    	if (mainVgaThum != null) {
    		File newFile = new File(imageFilePathStr + "VT_" 
    				+ mainImage.concat(getExtensionFromFileName(mainVgaThum.getFileFileName())));
        	CommUtil.copyFile(mainVgaThum.getFile(), newFile, true);
    	}
    	if (mainQVgaThum != null) {
    		File newFile = new File(imageFilePathStr + "QT_" 
    				+ mainImage.concat(getExtensionFromFileName(mainQVgaThum.getFileFileName())));
        	CommUtil.copyFile(mainQVgaThum.getFile(), newFile, true);
    	}
    	if (mainVgaDetail != null) {
    		File newFile = new File(imageFilePathStr + "VD_" 
    				+ mainImage.concat(getExtensionFromFileName(mainVgaDetail.getFileFileName())));
        	CommUtil.copyFile(mainVgaDetail.getFile(), newFile, true);
    	}
    	if (mainQVgaDetail != null) {
    		File newFile = new File(imageFilePathStr + "QD_" 
    				+ mainImage.concat(getExtensionFromFileName(mainQVgaDetail.getFileFileName())));
        	CommUtil.copyFile(mainQVgaDetail.getFile(), newFile, true);
    	}
    	if (mainVgaOriginal != null) {
    		File newFile = new File(imageFilePathStr + "VO_" 
    				+ mainImage.concat(getExtensionFromFileName(mainVgaOriginal.getFileFileName())));
        	CommUtil.copyFile(mainVgaOriginal.getFile(), newFile, true);
    	}
    	if (mainQVgaOriginal != null) {
    		File newFile = new File(imageFilePathStr + "QO_" 
    				+ mainImage.concat(getExtensionFromFileName(mainQVgaOriginal.getFileFileName())));
        	CommUtil.copyFile(mainQVgaOriginal.getFile(), newFile, true);
    	}
    	if (mainVgaOriginal2 != null) {
    		File newFile = new File(imageFilePathStr + "VO2_" 
    				+ mainImage.concat(getExtensionFromFileName(mainVgaOriginal2.getFileFileName())));
        	CommUtil.copyFile(mainVgaOriginal2.getFile(), newFile, true);
    	}
    	if (mainQVgaOriginal2 != null) {
    		File 	newFile = new File(imageFilePathStr + "QO2_" 
    				+ mainImage.concat(getExtensionFromFileName(mainQVgaOriginal2.getFileFileName())));
        	CommUtil.copyFile(mainQVgaOriginal2.getFile(), newFile, true);
    	}
    	if (mainVgaThum != null 
				&& mainQVgaThum != null 
				&& mainVgaDetail != null 
				&& mainQVgaDetail != null 
				&& mainVgaOriginal != null 
				&& mainVgaOriginal2 != null
				&& mainQVgaOriginal != null
				&& mainQVgaOriginal2 != null) {
    		fileMap.put("photo_main", mainImage.concat(getExtensionFromFileName(mainVgaThum.getFileFileName())));
    	    return true;
    	}
    	
    	return false;
	}
	
	private boolean moveReportMovie(ReportDto rBean, 
			String basicFileName, 
			HashMap<String, String> fileMap, 
			String movieFilePathStr, int num) throws Exception {
		String movieKey = "movie_".concat(String.valueOf(num));
		String movieDisc = "movie".concat(String.valueOf(num)).concat("_");
		FileBean movie = null;
    	FileBean movieVgaThum = null;
    	FileBean movieQVgaThum = null;
    	FileBean movieVgaDetail = null;
    	FileBean movieQVgaDetail = null;
        
    	switch (num) {
    	case 1 : 
    		movie = rBean.getMovie1();
    		movieVgaThum = rBean.getMovieVgaThum1();
    		movieQVgaThum = rBean.getMovieQVgaThum1();
    		movieVgaDetail = rBean.getMovieVgaDetail1();
    		movieQVgaDetail = rBean.getMovieQVgaDetail1();
    		break;
    	case 2 :
    		movie = rBean.getMovie2();
    		movieVgaThum = rBean.getMovieVgaThum2();
    		movieQVgaThum = rBean.getMovieQVgaThum2();
    		movieVgaDetail = rBean.getMovieVgaDetail2();
    		movieQVgaDetail = rBean.getMovieQVgaDetail2();
    		break;
    	case 3 :
    		movie = rBean.getMovie3();
    		movieVgaThum = rBean.getMovieVgaThum3();
    		movieQVgaThum = rBean.getMovieQVgaThum3();
    		movieVgaDetail = rBean.getMovieVgaDetail3();
    		movieQVgaDetail = rBean.getMovieQVgaDetail3();
    		break;
    	}
    	String movieName = movieDisc.concat(basicFileName);
    	if (movie != null) {
        	fileMap.put(movieKey, movieName.concat(getExtensionFromFileName(movie.getFileFileName())));
    		File newFile = new File(movieFilePathStr + fileMap.get(movieKey));
            CommUtil.copyFile(movie.getFile(), newFile, true);
    	}
    	String movieImage = movieDisc.concat(basicFileName);
    	if (movieVgaThum != null) {
    		File newFile = new File(movieFilePathStr + "VT_" 
    				+ movieImage.concat(getExtensionFromFileName(movieVgaThum.getFileFileName())));
            CommUtil.copyFile(movieVgaThum.getFile(), newFile, true);
    	}
    	if (movieQVgaThum != null) {
    		File newFile = new File(movieFilePathStr + "QT_" 
    				+ movieImage.concat(getExtensionFromFileName(movieQVgaThum.getFileFileName())));
            CommUtil.copyFile(movieQVgaThum.getFile(), newFile, true);
    	}
    	if (movieVgaDetail != null) {
    		File newFile = new File(movieFilePathStr + "VD_" 
    				+ movieImage.concat(getExtensionFromFileName(movieVgaDetail.getFileFileName())));
            CommUtil.copyFile(movieVgaDetail.getFile(), newFile, true);
    	}
    	if (movieQVgaDetail != null) {
    		File newFile = new File(movieFilePathStr + "QD_" 
    				+ movieImage.concat(getExtensionFromFileName(movieQVgaDetail.getFileFileName())));
            CommUtil.copyFile(movieQVgaDetail.getFile(), newFile, true);
    	}
    	if (movie != null
				&& movieVgaThum != null
				&& movieQVgaThum != null
				&& movieVgaDetail != null
				&& movieQVgaDetail != null) {
    		fileMap.put(movieKey.concat("_IMG"), movieImage.concat(getExtensionFromFileName(movieVgaThum.getFileFileName())));
    		return true; 
    	}
    	return false;
	}
	
    private String getExtensionFromFileName(String name) {
    	return (name.substring(name.lastIndexOf("."), name.length())).toLowerCase();
    }
    
    public void registeKijiContents(ReportDto rBean, HashMap<String, String> contents) throws Exception {
    	HashMap<String, Integer> point = dao.getContentsPoint();
    	CommonMstBean lBean = new CommonMstBean("03", "01", "01");
    	String location = dao.getCommonCode(lBean).getValue();
    	registContent(rBean, contents, "photo_cover", location, point.get("PH"), true);
    	
    	lBean = new CommonMstBean("03", "01", "02");
    	location = dao.getCommonCode(lBean).getValue();
    	registContent(rBean, contents, "photo_main", location, point.get("PH"), true);
    	
    	lBean = new CommonMstBean("03", "01", "03");
    	location = dao.getCommonCode(lBean).getValue();
    	registContent(rBean, contents, "photo_pickup", location, point.get("PH"), false);
    	
    	lBean = new CommonMstBean("03", "01", "04");
    	location = dao.getCommonCode(lBean).getValue();
    	registContent(rBean, contents, "movie_1", location, point.get("MO"), true);
    	
    	lBean = new CommonMstBean("03", "01", "05");
    	location = dao.getCommonCode(lBean).getValue();
    	registContent(rBean, contents, "movie_2", location, point.get("MO"), true);
    	
    	lBean = new CommonMstBean("03", "01", "06");
    	location = dao.getCommonCode(lBean).getValue();
    	registContent(rBean, contents, "movie_3", location, point.get("MO"), true);
    }
    
    private void registContent(ReportDto rBean, HashMap<String, String> contents, String disc, String location, int point, Boolean flag) throws Exception {
    	if (!contents.containsKey(disc)) return;
    	
    	String fileName = contents.get(disc);
    	String thumImgName = contents.get(disc.concat("_IMG"));
    	String kubunCode = "";
    	if (thumImgName == null) {
    		thumImgName = fileName;
    	}
    	
    	ContentMstBean bean = new ContentMstBean();
    	bean.setFile_name(fileName);
		bean.setThumb_vga_filename("VT_".concat(thumImgName));
    	bean.setThumb_qvga_filename("QT_".concat(thumImgName));
    	
    	if (disc.startsWith("photo")) {
        	bean.setMiddle_vga_filename("VD_".concat(thumImgName));
        	bean.setMiddle_qvga_filename("QD_".concat(thumImgName));
    		bean.setComplete_vga_filename("VO_".concat(thumImgName));
        	bean.setComplete_qvga_filename("QO_".concat(thumImgName));
        	bean.setComplete2_vga_filename("VO2_".concat(thumImgName));
        	bean.setComplete2_qvga_filename("QO2_".concat(thumImgName));
        	bean.setFile_path(globeMap.get("globe.path.photo").concat("/").concat(rBean.getTalent()).concat("/"));
        	CommonMstBean commMst = new CommonMstBean("03", "02", "01");
        	kubunCode = dao.getCommonCode(commMst).getValue();
    	} else {
    		bean.setFile_path(globeMap.get("globe.path.movie").concat("/").concat(rBean.getTalent()).concat("/"));
    		File downloadFile = new File(globeMap.get("globe.path.base").concat(bean.getFile_path()).concat(fileName));
        	if (downloadFile.exists()) {
        		bean.setFile_size(new BigDecimal(downloadFile.length()).intValue());
        	}
    	}
    	if (disc.equals("photo_main")) {
        	bean.setTitle(rBean.getMainTitle());
    	    bean.setComment(rBean.getComment());
    	} else if (disc.equals("movie_1")) {
    		bean.setTitle(rBean.getMovie1Title());
    		bean.setPlay_time(rBean.getMovie1PlayTime());
    		bean.setComment(rBean.getMovie1Comment());
    		kubunCode = rBean.getMovie1Kubun();
    	} else if (disc.equals("movie_2")) {
    		bean.setTitle(rBean.getMovie2Title());
    		bean.setPlay_time(rBean.getMovie2PlayTime());
    		bean.setComment(rBean.getMovie2Comment());
    		kubunCode = rBean.getMovie2Kubun();
    	} else if (disc.equals("movie_3")) {
    		bean.setTitle(rBean.getMovie3Title());
    		bean.setPlay_time(rBean.getMovie3PlayTime());
    		bean.setComment(rBean.getMovie3Comment());
    		kubunCode = rBean.getMovie3Kubun();
    	} else {
        	bean.setTitle(rBean.getTitle());
    	}
        bean.setShouhi_point(point);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        bean.setStart_date(sdf.parse(rBean.getStrDt()));
    	bean.setContent_kubun(kubunCode);
    	bean.setService_location_kubun(location);
    	dao.registeContents(bean);
    	String id = (String) dao.getLastestContentPk();

    	TalentContentBean tcBean = new TalentContentBean();
    	if (flag){
    		if(!rBean.getTalent1().equals("")){
    			tcBean.setTalent_id(Integer.parseInt(rBean.getTalent1()));
    			tcBean.setContent_id(Integer.parseInt(id));
    			dao.registeContentTalent(tcBean);
    		}
    		if(!rBean.getTalent2().equals("")){
		    	tcBean.setTalent_id(Integer.parseInt(rBean.getTalent2()));
		    	tcBean.setContent_id(Integer.parseInt(id));
		    	dao.registeContentTalent(tcBean);
    		}
	    	if(!rBean.getTalent3().equals("")){
		    	tcBean.setTalent_id(Integer.parseInt(rBean.getTalent3()));
		    	tcBean.setContent_id(Integer.parseInt(id));
		    	dao.registeContentTalent(tcBean);
	    	}
    	}else{
    		tcBean.setTalent_id(Integer.parseInt(rBean.getTalent()));
	    	tcBean.setContent_id(Integer.parseInt(id));
	    	dao.registeContentTalent(tcBean);
    	}
    	
    	TalentSenyouBean tsBean = new TalentSenyouBean();
    	if (flag){
    		if(!rBean.getTalent1().equals("")){
		    	tsBean.setKiji_id(Integer.parseInt(contents.get("kijiId")));
		    	tsBean.setContent_id(Integer.parseInt(id));
		    	tsBean.setTalent_id(Integer.parseInt(rBean.getTalent1()));
		    	dao.registeTalentSenyou(tsBean);
    		}
    		if(!rBean.getTalent2().equals("")){
		    	tsBean.setKiji_id(Integer.parseInt(contents.get("kijiId")));
		    	tsBean.setContent_id(Integer.parseInt(id));
		    	tsBean.setTalent_id(Integer.parseInt(rBean.getTalent2()));
		    	dao.registeTalentSenyou(tsBean);
    		}
    		if(!rBean.getTalent3().equals("")){
		    	tsBean.setKiji_id(Integer.parseInt(contents.get("kijiId")));
		    	tsBean.setContent_id(Integer.parseInt(id));
		    	tsBean.setTalent_id(Integer.parseInt(rBean.getTalent3()));
		    	dao.registeTalentSenyou(tsBean);
    		}
    	}else{
    		tsBean.setKiji_id(Integer.parseInt(contents.get("kijiId")));
	    	tsBean.setContent_id(Integer.parseInt(id));
	    	tsBean.setTalent_id(Integer.parseInt(rBean.getTalent()));
	    	dao.registeTalentSenyou(tsBean);
    	}
    }
    
    public List<TalentDto> getTalentList() throws Exception {
    	return dao.getTalentList();
    }
    
    public List<CommonMstBean> getMovieKubun() throws Exception {
    	return dao.getMovieKubun();
    }
    
    public String registeBagicReportInfo(ReportDto dto, String disc) throws Exception {
    	dto.setDisc(disc);
    	dao.registeBagicReportInfo(parseDto(dto));
        return (String) dao.getLastestPk();
    }
    
    private KijiBean parseDto(ReportDto dto) throws ParseException {
    	KijiBean bean = new KijiBean();
    	bean.setKiji_kubun(dto.getDisc());
    	bean.setKiji_title(dto.getTitle());
    	bean.setTalent_id(Integer.parseInt(dto.getTalent()));
    	bean.setVol_no(dto.getVolNo());
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        bean.setHyouji_kaisibi(sdf.parse(dto.getStrDt()));
    	return bean;
    }
    
    public ReportDetailDto getReportDetail(ReportDto rBean) throws Exception {
    	ReportDetailDto dto = new ReportDetailDto();
    	List<ReportDetailDto> rdList = dao.getReportDetail(rBean, "");
    	setReportDetailDto(rdList, dto);

		dto.setCoverImg("/content/NotFoundImage.jpg");
		dto.setMainImg("/content/NotFoundImage.jpg");
		dto.setPickUpImg("/content/NotFoundImage.jpg");
		dto.setMovie1Img("/content/NotFoundImage.jpg");
		dto.setMovie2Img("/content/NotFoundImage.jpg");
		dto.setMovie3Img("/content/NotFoundImage.jpg");
		
    	List<ReportDetailContentDto> kijiContent = dao.getKijiContent(rBean);
    	for (ReportDetailContentDto cInfo : kijiContent) {
    		String disc = cInfo.getService_location_kubun();
    		String playTime = cInfo.getPlay_time();
    		playTime = playTime == null || playTime.trim().equals("") ? "00:00:00" : playTime; 
    		
    		String fileName = cInfo.getThumb_vga_fileName();
    		if (disc.equals("CO")) {
    			dto.setCoverImg(cInfo.getFile_path().concat(fileName));
    		} else if (disc.equals("MA")) {
    			dto.setMainTitle(cInfo.getTitle());
    			dto.setMainImg(cInfo.getFile_path().concat(fileName));
    			dto.setComment(cInfo.getComment());
    		} else if (disc.equals("PK")) {
    			dto.setPickUpImg(cInfo.getFile_path().concat(fileName));
    		} else if (disc.equals("M1")) {
    			dto.setMovie1Title(cInfo.getTitle());
    			dto.setMovie1Img(cInfo.getFile_path().concat(fileName));
    			dto.setMovie1PlayTime(playTime);
    			dto.setMovie1Comment(cInfo.getComment());
    		} else if (disc.equals("M2")) {
    			dto.setMovie2Title(cInfo.getTitle());
    			dto.setMovie2Img(cInfo.getFile_path().concat(fileName));
    			dto.setMovie2PlayTime(playTime);
    			dto.setMovie2Comment(cInfo.getComment());
    		} else if (disc.equals("M3")) {
    			dto.setMovie3Title(cInfo.getTitle());
    			dto.setMovie3Img(cInfo.getFile_path().concat(fileName));
    			dto.setMovie3PlayTime(playTime);
    			dto.setMovie3Comment(cInfo.getComment());
    		}
    	}
    	
    	return dto;
    }
    
    private void setReportDetailDto(List<ReportDetailDto> rdList,
			ReportDetailDto dto) {
		// TODO Auto-generated method stub
    	dto.setId(rdList.get(0).getId());
    	dto.setDisc(rdList.get(0).getDisc());
    	dto.setTitle(rdList.get(0).getTitle());
    	dto.setVolNo(rdList.get(0).getVolNo());
    	dto.setStrDt(rdList.get(0).getStrDt());
    	int i = 0;
    	for (ReportDetailDto reportDetailDto : rdList) {
    		if (i == 0){
    			dto.setTalent1(reportDetailDto.getTalent());
    			dto.setTalentKana1(reportDetailDto.getTalentKana());
    		}
    		if (i == 1){
    			dto.setTalent2(reportDetailDto.getTalent());
    			dto.setTalentKana2(reportDetailDto.getTalentKana());
    		}
    		if (i == 2){
    			dto.setTalent3(reportDetailDto.getTalent());
    			dto.setTalentKana3(reportDetailDto.getTalentKana());
    		}
    		
    		i++;
		}
	}
	public ReportDto getReportModifyDetail(ReportDto rBean) throws Exception {
		
		ReportDetailDto dto = new ReportDetailDto();
    	List<ReportDetailDto> rdList = dao.getReportDetail(rBean, "");
    	setReportDetailDto(rdList, dto);
    	
//    	ReportDetailDto dto = dao.getReportDetail(rBean);
    	rBean.setTitle(dto.getTitle());
    	rBean.setId(dto.getId());
    	rBean.setTalent1(dto.getTalent1());
    	rBean.setTalent2(dto.getTalent2());
    	rBean.setTalent3(dto.getTalent3());
    	rBean.setTalentKana1(dto.getTalentKana1());
    	rBean.setTalentKana2(dto.getTalentKana2());
    	rBean.setTalentKana3(dto.getTalentKana3());
    	rBean.setVolNo(dto.getVolNo());
    	rBean.setStrDt(dto.getStrDt());
    	
    	rBean.setCoverImg("/content/NotFoundImage.jpg");
    	rBean.setMainImg("/content/NotFoundImage.jpg");
    	rBean.setPickUpImg("/content/NotFoundImage.jpg");
    	rBean.setMovie1Img("/content/NotFoundImage.jpg");
    	rBean.setMovie2Img("/content/NotFoundImage.jpg");
    	rBean.setMovie3Img("/content/NotFoundImage.jpg");
    	List<ReportDetailContentDto> kijiContent = dao.getKijiContent(rBean);    	
    	for (ReportDetailContentDto cInfo : kijiContent) {
    		String disc = cInfo.getService_location_kubun();
    		String playTime = cInfo.getPlay_time();
    		playTime = playTime == null || playTime.trim().equals("") ? "00:00:00" : playTime; 

			String fileName = cInfo.getThumb_vga_fileName();
    		if (disc.equals("CO")) {
    			rBean.setCoverImg(cInfo.getFile_path().concat(fileName));
    		} else if (disc.equals("MA")) {
    			rBean.setMainTitle(cInfo.getTitle());
    			rBean.setMainImg(cInfo.getFile_path().concat(fileName));
    			rBean.setComment(cInfo.getComment());
    		} else if (disc.equals("PK")) {
    			rBean.setPickUpImg(cInfo.getFile_path().concat(fileName));
    		} else if (disc.equals("M1")) {
    			rBean.setMovie1Title(cInfo.getTitle());
    			rBean.setMovie1Img(cInfo.getFile_path().concat(fileName));
    			rBean.setMovie1PlayTime(playTime);
    			rBean.setMovie1Comment(cInfo.getComment());
    			rBean.setMovie1Kubun(cInfo.getContent_kubun());
    		} else if (disc.equals("M2")) {
    			rBean.setMovie2Title(cInfo.getTitle());
    			rBean.setMovie2Img(cInfo.getFile_path().concat(fileName));
    			rBean.setMovie2PlayTime(playTime);
    			rBean.setMovie2Comment(cInfo.getComment());
    			rBean.setMovie2Kubun(cInfo.getContent_kubun());
    		} else if (disc.equals("M3")) {
    			rBean.setMovie3Title(cInfo.getTitle());
    			rBean.setMovie3Img(cInfo.getFile_path().concat(fileName));
    			rBean.setMovie3PlayTime(playTime);
    			rBean.setMovie3Comment(cInfo.getComment());
    			rBean.setMovie3Kubun(cInfo.getContent_kubun());
    		}
    	}
    	return rBean;
    }
    
    public void updateReport(ReportDto rBean) throws Exception {
    	dao.updateReportBasicInfo(rBean);
    	HashMap<String, String> fileMap = new HashMap<String, String>();
    	fileMap.put("kijiId", rBean.getId());
    	String filePath = globeMap.get("globe.path.commonImg");//"C:/eclipse/workspace/content/";
    	String basicFileName = rBean.getId().concat("_").concat(rBean.getTalent());
    	
    	//コンテンツ保存ディレクトリ生成
    	String imageFilePathStr = filePath.concat(globeMap.get("globe.path.photo")).concat("/").concat(rBean.getTalent()).concat("/");
    	File imageFilePath = new File(imageFilePathStr);
    	if (!imageFilePath.exists()) {
    		imageFilePath.mkdirs();
    	}
    	String movieFilePathStr = filePath.concat(globeMap.get("globe.path.movie")).concat("/").concat(rBean.getTalent()).concat("/");
    	File movieFilePath = new File(movieFilePathStr);
    	if (!movieFilePath.exists()) {
    		movieFilePath.mkdirs();
    	}
    	
		boolean coverInput = moveReportCoverImg(rBean, basicFileName, fileMap, imageFilePathStr);
		boolean mainInput = moveReportMainImg(rBean, basicFileName, fileMap, imageFilePathStr);
		boolean pickupInput = movePickupImg(rBean, basicFileName, fileMap, imageFilePathStr);
		boolean movie1Input = moveReportMovie(rBean, basicFileName, fileMap, movieFilePathStr, 1);
		boolean movie2Input = moveReportMovie(rBean, basicFileName, fileMap, movieFilePathStr, 2);
		boolean movie3Input = moveReportMovie(rBean, basicFileName, fileMap, movieFilePathStr, 3);
    	
		HashMap<String, Integer> point = dao.getContentsPoint();
    	List<ReportDetailContentDto> kijiContent = dao.getKijiContent(rBean);
    	
    	int coverId = containContent(kijiContent, "CO");
    	if (coverInput && coverId == -1) {
    		CommonMstBean lBean = new CommonMstBean("03", "01", "01");
        	String location = dao.getCommonCode(lBean).getValue();
    		registeModifyContent(rBean, fileMap, "photo_cover", location, point.get("PH"), false);
    	}
    	
    	int mainId = containContent(kijiContent, "MA");
        if (mainInput && mainId == -1) {
        	CommonMstBean lBean = new CommonMstBean("03", "01", "02");
        	String location = dao.getCommonCode(lBean).getValue();
    		registeModifyContent(rBean, fileMap, "photo_main", location, point.get("PH"), true);
    	} else {
    		modifyKijiContent(rBean, mainId, "MA", fileMap);
    	}
        
        int pickId = containContent(kijiContent, "PK");
        if (pickupInput && pickId == -1) {
        	CommonMstBean lBean = new CommonMstBean("03", "01", "03");
        	String location = dao.getCommonCode(lBean).getValue();
    		registeModifyContent(rBean, fileMap, "photo_pickup", location, point.get("PH"), false);
    	}
        
        int movie1Id = containContent(kijiContent, "M1");
        if (movie1Input && movie1Id == -1) {
        	CommonMstBean lBean = new CommonMstBean("03", "01", "04");
        	String location = dao.getCommonCode(lBean).getValue();
    		registeModifyContent(rBean, fileMap, "movie_1", location, point.get("MO") ,true);
    	} else if (movie1Id != -1) {
    		modifyKijiContent(rBean, movie1Id, "M1", fileMap);
    	}
        
        int movie2Id = containContent(kijiContent, "M2");
        if (movie2Input && movie2Id == -1) {
        	CommonMstBean lBean = new CommonMstBean("03", "01", "05");
        	String location = dao.getCommonCode(lBean).getValue();
    		registeModifyContent(rBean, fileMap, "movie_2", location, point.get("MO"), true);
    	} else if (movie2Id != -1) {
    		modifyKijiContent(rBean, movie2Id, "M2", fileMap);
    	}
        
        int movie3Id = containContent(kijiContent, "M3");
        if (movie3Input && movie3Id == -1) {
        	CommonMstBean lBean = new CommonMstBean("03", "01", "06");
        	String location = dao.getCommonCode(lBean).getValue();
    		registeModifyContent(rBean, fileMap, "movie_3", location, point.get("MO"), true);
    	} else if (movie3Id != -1) {
    		modifyKijiContent(rBean, movie3Id, "M3", fileMap);
    	}
        //メインイメージ説明修正
        dao.updateKijiMainImageComment(rBean);
        //コンテンツ表示開始日修正
        dao.updateKijiContentStartDate(rBean);
        //コンテンツタイトルを修正
        //dao.updateKijiContentTitle(rBean);
    }
    
    private void modifyKijiContent(ReportDto rBean, int contentId, String disc, HashMap<String, String> fileMap) throws SQLException {
    	ContentMstBean bean = new ContentMstBean();
		bean.setContent_id(contentId);
		String moviePath = globeMap.get("globe.path.base").concat(globeMap.get("globe.path.movie")).concat("/").concat(rBean.getTalent()).concat("/");
		if (disc.equals("M1")) {
			bean.setTitle(rBean.getMovie1Title());
			bean.setComment(rBean.getMovie1Comment());
			bean.setPlay_time(rBean.getMovie1PlayTime());
			bean.setContent_kubun(rBean.getMovie1Kubun());
			if (fileMap.containsKey("movie_1")) {
				File movie1 = new File(moviePath.concat(fileMap.get("movie_1")));
				bean.setFile_size(new BigDecimal(movie1.length()).intValue());
			} else {
				bean.setFile_size(-1);
			}
		} else if (disc.equals("M2")) {
			bean.setTitle(rBean.getMovie2Title());
			bean.setComment(rBean.getMovie2Comment());
			bean.setPlay_time(rBean.getMovie2PlayTime());
			bean.setContent_kubun(rBean.getMovie2Kubun());
			if (fileMap.containsKey("movie_2")) {
				File movie2 = new File(moviePath.concat(fileMap.get("movie_2")));
				bean.setFile_size(new BigDecimal(movie2.length()).intValue());
			} else {
				bean.setFile_size(-1);
			}
		} else if (disc.equals("M3")) {
			bean.setTitle(rBean.getMovie3Title());
			bean.setComment(rBean.getMovie3Comment());
			bean.setPlay_time(rBean.getMovie3PlayTime());
			bean.setContent_kubun(rBean.getMovie3Kubun());
			if (fileMap.containsKey("movie_3")) {
				File movie3 = new File(moviePath.concat(fileMap.get("movie_3")));
				bean.setFile_size(new BigDecimal(movie3.length()).intValue());
			} else {
				bean.setFile_size(-1);
			}
		} else if (disc.equals("MA")) {
			bean.setTitle(rBean.getMainTitle());
			bean.setComment(rBean.getComment());
			bean.setContent_kubun("PH");
		}
		dao.updateKijiContent(bean);
    }
    
    private void registeModifyContent(ReportDto rBean, HashMap<String, String> fileMap, String disc, String location, int point, Boolean flag) throws ParseException, SQLException {
    	String fileName = fileMap.get(disc);
    	String thumImgName = fileMap.get(disc.concat("_IMG"));
    	if (thumImgName == null) {
    		thumImgName = fileName;
    	}
    	String kubunCode = "";
    	ContentMstBean bean = new ContentMstBean();
		
		bean.setFile_name(fileName);
		bean.setThumb_vga_filename("VT_".concat(thumImgName));
    	bean.setThumb_qvga_filename("QT_".concat(thumImgName));
    	bean.setMiddle_vga_filename("VD_".concat(thumImgName));
    	bean.setMiddle_qvga_filename("QD_".concat(thumImgName));
		
    	if (disc.startsWith("photo")) {
    		bean.setComplete_vga_filename("VO_".concat(thumImgName));
        	bean.setComplete_qvga_filename("QO_".concat(thumImgName));
        	if (!disc.equals("photo_cover") && !disc.equals("photo_cover")) {
        		bean.setComplete2_vga_filename("VO2_".concat(thumImgName));
            	bean.setComplete2_qvga_filename("QO2_".concat(thumImgName));
        	}
        	bean.setFile_path(globeMap.get("globe.path.photo").concat("/").concat(rBean.getTalent()).concat("/"));
        	CommonMstBean commMst = new CommonMstBean("03", "02", "01");
        	kubunCode = dao.getCommonCode(commMst).getValue();
    	} else {
    		bean.setFile_path(globeMap.get("globe.path.movie").concat("/").concat(rBean.getTalent()).concat("/"));
    		File downloadFile = new File(globeMap.get("globe.path.base").concat(bean.getFile_path()).concat(fileName));
        	if (downloadFile.exists()) {
        		bean.setFile_size(new BigDecimal(downloadFile.length()).intValue());
        	}
    	}
    	if (disc.equals("photo_main")) {
			bean.setTitle(rBean.getMainTitle());
    	    bean.setComment(rBean.getComment());
    	} else if (disc.equals("movie_1")) {
    		bean.setTitle(rBean.getMovie1Title());
    		bean.setPlay_time(rBean.getMovie1PlayTime());
    		kubunCode = rBean.getMovie1Kubun();
    		bean.setComment(rBean.getMovie1Comment());
    	} else if (disc.equals("movie_2")) {
    		bean.setTitle(rBean.getMovie2Title());
    		bean.setPlay_time(rBean.getMovie2PlayTime());
    		kubunCode = rBean.getMovie2Kubun();
    		bean.setComment(rBean.getMovie2Comment());
    	} else if (disc.equals("movie_3")) {
    		bean.setTitle(rBean.getMovie3Title());
    		bean.setPlay_time(rBean.getMovie3PlayTime());
    		kubunCode = rBean.getMovie3Kubun();
    		bean.setComment(rBean.getMovie3Comment());
    	} else {
    		bean.setTitle(rBean.getTitle());
    	}
    
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	bean.setStart_date(sdf.parse(rBean.getStrDt()));
    	bean.setShouhi_point(point);
		bean.setContent_kubun(kubunCode);
		bean.setService_location_kubun(location);
		dao.registeContents(bean);
		String id = (String) dao.getLastestContentPk();
		
		TalentContentBean tcBean = new TalentContentBean();
    	if (flag){
    		if(!rBean.getTalent1().equals("")){
    			tcBean.setTalent_id(Integer.parseInt(rBean.getTalent1()));
    			tcBean.setContent_id(Integer.parseInt(id));
    			dao.registeContentTalent(tcBean);
    		}
    		if(!rBean.getTalent2().equals("")){
		    	tcBean.setTalent_id(Integer.parseInt(rBean.getTalent2()));
		    	tcBean.setContent_id(Integer.parseInt(id));
		    	dao.registeContentTalent(tcBean);
    		}
	    	if(!rBean.getTalent3().equals("")){
		    	tcBean.setTalent_id(Integer.parseInt(rBean.getTalent3()));
		    	tcBean.setContent_id(Integer.parseInt(id));
		    	dao.registeContentTalent(tcBean);
	    	}
    	}else{
    		tcBean.setTalent_id(Integer.parseInt(rBean.getTalent()));
	    	tcBean.setContent_id(Integer.parseInt(id));
	    	dao.registeContentTalent(tcBean);
    	}
    	
    	TalentSenyouBean tsBean = new TalentSenyouBean();
    	if (flag){
    		if(!rBean.getTalent1().equals("")){
		    	tsBean.setKiji_id(Integer.parseInt(rBean.getId()));
		    	tsBean.setContent_id(Integer.parseInt(id));
		    	tsBean.setTalent_id(Integer.parseInt(rBean.getTalent1()));
		    	dao.registeTalentSenyou(tsBean);
    		}
    		if(!rBean.getTalent2().equals("")){
		    	tsBean.setKiji_id(Integer.parseInt(rBean.getId()));
		    	tsBean.setContent_id(Integer.parseInt(id));
		    	tsBean.setTalent_id(Integer.parseInt(rBean.getTalent2()));
		    	dao.registeTalentSenyou(tsBean);
    		}
    		if(!rBean.getTalent3().equals("")){
		    	tsBean.setKiji_id(Integer.parseInt(rBean.getId()));
		    	tsBean.setContent_id(Integer.parseInt(id));
		    	tsBean.setTalent_id(Integer.parseInt(rBean.getTalent3()));
		    	dao.registeTalentSenyou(tsBean);
    		}
    	}else{
    		tsBean.setKiji_id(Integer.parseInt(rBean.getId()));
	    	tsBean.setContent_id(Integer.parseInt(id));
	    	tsBean.setTalent_id(Integer.parseInt(rBean.getTalent()));
	    	dao.registeTalentSenyou(tsBean);
    	}
    	
    }
    
    private int containContent(List<ReportDetailContentDto> kijiContent, String kubun) {
    	for (ReportDetailContentDto cInfo : kijiContent) {
    		String disc = cInfo.getService_location_kubun();
    		if (disc.equals(kubun)) return Integer.parseInt(cInfo.getContent_id());
    	}
    	return -1;
    }
    
    public void registeDigestMain(DigestDto dBean, ReportDto rBean, String digestImg, ReportDetailDto dto, int point) throws SQLException, ParseException {
    	ContentMstBean bean = new ContentMstBean();
    	{
    		CommonMstBean cmb = new CommonMstBean("03", "02", "01");
        	cmb = dao.getCommonCode(cmb);
    		bean.setContent_kubun(cmb.getValue());//PH
    	}
		bean.setTitle(dBean.getHeaderImgTitle());
		bean.setFile_name(digestImg);
		bean.setThumb_vga_filename("VT_".concat(digestImg));
    	bean.setThumb_qvga_filename("QT_".concat(digestImg));
    	bean.setMiddle_vga_filename("VD_".concat(digestImg));
    	bean.setMiddle_qvga_filename("QD_".concat(digestImg));
		bean.setComplete_vga_filename("VO_".concat(digestImg));
    	bean.setComplete_qvga_filename("QO_".concat(digestImg));
    	bean.setComplete2_vga_filename("VO2_".concat(digestImg));
    	bean.setComplete2_qvga_filename("QO2_".concat(digestImg));
    	if (dBean.getTalent1() != null 
    			&& !dBean.getTalent1().equals("")) {
    		bean.setFile_path("/content/photo/".concat(dBean.getTalent1()).concat("/"));
    	} else {
        	bean.setFile_path("/content/photo/".concat(dto.getTalent()).concat("/"));
    	}
    	{
    		CommonMstBean cmb = new CommonMstBean("03", "01", "07");
        	cmb = dao.getCommonCode(cmb);
    		bean.setService_location_kubun(cmb.getValue());//DG
    	}
    	bean.setComment(dBean.getNaiyou());
    	bean.setShouhi_point(point);
    	bean.setDigest_location_seq(0);
    	
    	ReportDetailDto temp = dao.getReportDetail(rBean);
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	bean.setStart_date(sdf.parse(temp.getStrDt()));
		dao.registeContents(bean);
		String id = (String) dao.getLastestContentPk();

		TalentContentBean tcBean = new TalentContentBean();
		if (dBean.getTalent1() != null 
    			&& !dBean.getTalent1().equals("")) {
	    	tcBean.setTalent_id(Integer.parseInt(dBean.getTalent1()));
		} else {
	    	tcBean.setTalent_id(Integer.parseInt(dto.getTalent()));
		}
    	tcBean.setContent_id(Integer.parseInt(id));
    	dao.registeContentTalent(tcBean);
    	
    	if (dBean.getTalent2() != null 
    			&& !dBean.getTalent2().equals("")
    			&& !dBean.getTalent2().equals(dBean.getTalent1())) {
    		tcBean = new TalentContentBean();
        	tcBean.setTalent_id(Integer.parseInt(dBean.getTalent2()));
        	tcBean.setContent_id(Integer.parseInt(id));
        	dao.registeContentTalent(tcBean);
    	}
    	
    	if (dBean.getTalent3() != null 
    			&& !dBean.getTalent3().equals("")
    			&& !dBean.getTalent3().equals(dBean.getTalent1()) 
    			&& !dBean.getTalent3().equals(dBean.getTalent2())) {
    		tcBean = new TalentContentBean();
        	tcBean.setTalent_id(Integer.parseInt(dBean.getTalent3()));
        	tcBean.setContent_id(Integer.parseInt(id));
        	dao.registeContentTalent(tcBean);
    	}
    	
    	KijiDigestContentBean kdcBean = new KijiDigestContentBean();
    	kdcBean.setKIJI_DIGEST_ID(Integer.parseInt(dBean.getId()));
    	kdcBean.setCONTENT_ID(Integer.parseInt(id));
    	dao.registeKijiDigestContent(kdcBean);
    }
    
    public void registeDigest(DigestDto dBean, ReportDto rBean, int point) throws Exception {
    	dao.registeDigest(parseKijiDiagest(dBean));
    	dBean.setId((String) dao.getLastestDigestPk());
    	ReportDetailDto dto = dao.getReportDetail(rBean);
    	String digestImg = moveDigestContents(dBean, dto.getTalent());
    	if (digestImg == null) return;
    	
    	//コンテンツ登録
    	registeDigestMain(dBean, rBean, digestImg, dto, point);
    }
    
    private KijiDigestBean parseKijiDiagest(DigestDto dBean) {
    	KijiDigestBean bean = new KijiDigestBean();
    	bean.setTITLE(dBean.getTitle());
    	bean.setNAIYOU(dBean.getContents());
    	bean.setKIJI_ID(dBean.getKijiId());
    	return bean;
    }
    
    public List<DigestDto> getDigestList(String kijiId) throws SQLException {
    	return dao.selectDigestList(kijiId);
    }
    
    public DigestDto getDigestDetail(String digestId, boolean modifyFlg) throws SQLException, ParserConfigurationException, SAXException, IOException, TransformerException {
    	DigestDto dto = dao.selectDigestDetail(digestId);
    	List<TalentDto> tList = dao.getTalentContent(dto.getContentId());
    	if (dto.getContentId().equals("")) {
    		dto.setMainImg(true);
    	}
    	if (tList.size() > 0) {
        	dto.setTalent1Kana(tList.get(0).getTalent_kana());
    	}
    	if (tList.size() > 1) {
    		dto.setTalent2Kana(tList.get(1).getTalent_kana());
    	}
    	if (tList.size() > 2) {
    		dto.setTalent3Kana(tList.get(2).getTalent_kana());
    	}
    	if (!dto.getFilePath().trim().equals("") && !dto.getThumbVgaFileName().trim().equals("")) {
    		dto.setDigestImgFile(dto.getFilePath().concat(dto.getThumbVgaFileName()));
    	} else {
    		dto.setDigestImgFile("/content/NotFoundImage.jpg");
    	}
    	if (!modifyFlg) {
    		dto.setContents(CommUtil.deleteContentLinkPath(dto.getContents()));
    	}
    	//dto.setContents(dto.getContents());
    	return dto;
    }
    
    public void updateDigest(DigestDto dBean, ReportDto rBean, int point) throws Exception {
    	List<DigestContentImageDto> dContents = getUploadedFileList();
    	ReportDetailDto dto = dao.getReportDetail(rBean);
    	
    	String mainImg = moveDigestContents(dBean, dto.getTalent());
    	String isDe = dao.getDigestContent(dBean.getId());
    	if (isDe == null && mainImg != null) {
    		registeDigestMain(dBean, rBean, mainImg, dto, point);
    	}
        
    	int digestIndex = selectDigestMaxSeq(dBean.getId());
    	if (dContents != null) {
	    	dContents = parseContentImgTag(dBean, dContents, digestIndex);
	    	moveDigestContents(rBean, dBean, dContents, digestIndex, point);
    	}
    	updateDigestContents(dBean);
    }
    public void updateDigestContents(DigestDto dBean) throws SQLException {
        dao.updateDigestDetail(dBean);
    }
    public int selectDigestMaxSeq(String id) throws SQLException {
    	return (Integer) dao.getDigestMaxSeq(id);
	}
    public List<DigestContentImageDto> parseContentImgTag(
    		DigestDto dBean, List<DigestContentImageDto> dContents, int digestIndex) {
    	String contents = dBean.getContents();
        List<ImgTag> imgTags = getImgTags(contents);
        
        List<DigestContentImageDto> usedImg = new ArrayList<DigestContentImageDto>();
        int index = 0;
        for (ImgTag imgTag : imgTags) {
        	String src = imgTag.getSrc();
        	if (src.equals("")) continue;
        	String filePath = globeMap.get("globe.path.photo").concat("/");//"/content/photo/";
        	for (DigestContentImageDto dto : dContents) {
        		if (dto.getVgaThum().equals(src)) {
        			filePath = filePath.concat(dto.getTalent1()).concat("/");
        			String basic = "VT_digest".concat(String.valueOf(index+digestIndex)).concat(
        					"_").concat(dBean.getId()).concat("_").concat(dto.getTalent1()).concat(
        							getExtensionFromFileName(src));
        			usedImg.add(dto);
        			contents = contents.replaceFirst(src, filePath.concat(basic));
        			dto.setRealPath(filePath.concat(basic));
        			index++;
        			break;
        		}
        	}
        }
        dBean.setContents(contents);
        return usedImg;
    }
    
    private List<ImgTag> getImgTags(String contents) {
    	List<ImgTag> imgTags = new ArrayList<ImgTag>();
    	Pattern p = Pattern.compile("<img[^<>]*/>");
    	Matcher m = p.matcher(contents);
    	while (m.find()) {
    		ImgTag imgTag = new ImgTag(contents.substring(m.start(), m.end()));
    		imgTags.add(imgTag);
    	}
    	return imgTags;
    }
    
    private static class ImgTag {
    	private String tag;
    	public ImgTag(String tag) {
    		this.tag = tag;
    	}
    	public String getSrc() {
    		Pattern p = Pattern.compile("src[ ]*=[ ]*\".*\"");
    		Matcher m = p.matcher(tag);
    		if (m.find()) {
    			String src = tag.substring(m.start(), m.end());
    			src = src.substring(src.indexOf("\"")+1, src.lastIndexOf("\""));
    			return src;
    		}
    		return "";
    	}
    }
    
    public void moveDigestContents(ReportDto rBean, DigestDto dBean, List<DigestContentImageDto> dContents, int digestIndex, int point) throws Exception {
    	String filePath = globeMap.get("globe.path.commonImg").concat(globeMap.get("globe.path.photo")).concat("/");//"C:/eclipse/workspace/content/photo/";
    	for (int i = 0; i < dContents.size(); i++) {
    		DigestContentImageDto dto = dContents.get(i);
    		String contentPath = filePath.concat(dto.getTalent1()).concat("/");
    		String basicFileName = "digest".concat(String.valueOf(digestIndex + i)).concat("_").concat(dBean.getId()).concat("_").concat(dto.getTalent1()).concat(getExtensionFromFileName(dto.getVgaThum()));
    		
    		File dic = new File(contentPath);
    		if (!dic.exists()) {
    			dic.mkdirs();
    		}
    		{
	    		File oldFile = new File(globeMap.get("globe.path.root").concat("/").concat(dto.getVgaThum()));
	    		File newFile = new File(contentPath.concat("VT_").concat(basicFileName));
	    		CommUtil.copyFile(oldFile, newFile, true);
    		}
    		{
    			File oldFile = new File(globeMap.get("globe.path.root").concat("/").concat(dto.getQvgaThum()));
	    		File newFile = new File(contentPath.concat("QT_").concat(basicFileName));
	    		CommUtil.copyFile(oldFile, newFile, true);
    		}
    		{
    			File oldFile = new File(globeMap.get("globe.path.root").concat("/").concat(dto.getVgaDetail()));
	    		File newFile = new File(contentPath.concat("VD_").concat(basicFileName));
	    		CommUtil.copyFile(oldFile, newFile, true);
    		}
    		{
    			File oldFile = new File(globeMap.get("globe.path.root").concat("/").concat(dto.getQvgaDetail()));
	    		File newFile = new File(contentPath.concat("QD_").concat(basicFileName));
	    		CommUtil.copyFile(oldFile, newFile, true);
    		}
    		{
    			File oldFile = new File(globeMap.get("globe.path.root").concat("/").concat(dto.getVgaOriginal()));
	    		File newFile = new File(contentPath.concat("VO_").concat(basicFileName));
	    		CommUtil.copyFile(oldFile, newFile, true);
    		}
    		{
    			File oldFile = new File(globeMap.get("globe.path.root").concat("/").concat(dto.getQvgaOriginal()));
	    		File newFile = new File(contentPath.concat("QO_").concat(basicFileName));
	    		CommUtil.copyFile(oldFile, newFile, true);
    		}
    		{
    			File oldFile = new File(globeMap.get("globe.path.root").concat("/").concat(dto.getVgaOriginal2()));
	    		File newFile = new File(contentPath.concat("VO2_").concat(basicFileName));
	    		CommUtil.copyFile(oldFile, newFile, true);
    		}
    		{
    			File oldFile = new File(globeMap.get("globe.path.root").concat("/").concat(dto.getQvgaOriginal2()));
	    		File newFile = new File(contentPath.concat("QO2_").concat(basicFileName));
	    		CommUtil.copyFile(oldFile, newFile, true);
    		}
    		
        	//コンテンツ登録
        	ContentMstBean bean = new ContentMstBean();
        	{
        		CommonMstBean cmb = new CommonMstBean("03", "02", "01");
        		cmb = dao.getCommonCode(cmb);
        		bean.setContent_kubun(cmb.getValue());//PH
        	}
    		bean.setTitle(dto.getTitle());
    		bean.setFile_name(basicFileName);
    		bean.setThumb_vga_filename("VT_".concat(basicFileName));
        	bean.setThumb_qvga_filename("QT_".concat(basicFileName));
        	bean.setMiddle_vga_filename("VD_".concat(basicFileName));
        	bean.setMiddle_qvga_filename("QD_".concat(basicFileName));
    		bean.setComplete_vga_filename("VO_".concat(basicFileName));
        	bean.setComplete_qvga_filename("QO_".concat(basicFileName));
        	bean.setComplete2_vga_filename("VO2_".concat(basicFileName));
        	bean.setComplete2_qvga_filename("QO2_".concat(basicFileName));
        	bean.setFile_path(globeMap.get("globe.path.photo").concat("/").concat(dto.getTalent1()).concat("/"));
        	{
        		CommonMstBean cmb = new CommonMstBean("03", "01", "07");
        		cmb = dao.getCommonCode(cmb);
        		bean.setService_location_kubun(cmb.getValue().concat(String.valueOf(digestIndex + i)));
        	}
        	bean.setDigest_location_seq(digestIndex + i);
        	
        	ReportDetailDto temp = dao.getReportDetail(rBean);
        	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        	bean.setStart_date(sdf.parse(temp.getStrDt()));
        	bean.setComment(dto.getComment());
        	bean.setShouhi_point(point);
    		dao.registeContents(bean);
    		String id = (String) dao.getLastestContentPk();
    		
    		TalentContentBean tcBean = new TalentContentBean();
        	tcBean.setTalent_id(Integer.parseInt(dto.getTalent1()));
        	tcBean.setContent_id(Integer.parseInt(id));
        	dao.registeContentTalent(tcBean);
        	
        	if (dto.getTalent2() != null && !dto.getTalent2().trim().equals("")) {
        		tcBean = new TalentContentBean();
            	tcBean.setTalent_id(Integer.parseInt(dto.getTalent2()));
            	tcBean.setContent_id(Integer.parseInt(id));
            	dao.registeContentTalent(tcBean);
        	}
        	
        	if (dto.getTalent3() != null && !dto.getTalent3().trim().equals("")) {
        		tcBean = new TalentContentBean();
            	tcBean.setTalent_id(Integer.parseInt(dto.getTalent3()));
            	tcBean.setContent_id(Integer.parseInt(id));
            	dao.registeContentTalent(tcBean);
        	}
        	
        	KijiDigestContentBean kdcBean = new KijiDigestContentBean();
        	kdcBean.setKIJI_DIGEST_ID(Integer.parseInt(dBean.getId()));
        	kdcBean.setCONTENT_ID(Integer.parseInt(id));
        	dao.registeKijiDigestContent(kdcBean);
        	
        	String content = dBean.getContents();
        	Pattern p = Pattern.compile("<img[^<>]*src=\""+dto.getRealPath()+"\"[^<>]*/>");
        	Matcher m = p.matcher(content);
        	if (m.find()) {
        		String tag = content.substring(m.start(), m.end());
        		content = m.replaceFirst("<a href=\"coverPic.action?uid=NULLGWDOCOMO&cDTO.content_id="+id+"\">" + tag + "</a>");
        	}
        	dBean.setContents(content);
    	}
    }
    
    public void clearTempDigestImg() {
    	String path = globeMap.get("globe.path.root").concat(globeMap.get("golbe.path.tempDigestImgPath"));
    	File tempPath = new File(path);
    	if (tempPath.exists()) {
    		String[] tempImgs = tempPath.list();
        	for (String tempImg : tempImgs) {
        		new File(path.concat(tempImg)).delete();
        	}
    	}
    }
    
    public String moveDigestContents(DigestDto dBean, String talent) throws Exception {
    	//"C:/eclipse/workspace/content/photo/";
    	String filePath = globeMap.get("globe.path.commonImg").concat(globeMap.get("globe.path.photo")).concat("/");
    	dao.updateDigestImageComment(dBean);
    	if (dBean.getTalent1() != null && !dBean.getTalent1().equals("")) {
    		talent = dBean.getTalent1();
    	}
    	String basicFileName = dBean.getId().concat("_").concat(talent);
    	//コンテンツ保存ディレクトリ生成
    	String imageFilePathStr = filePath.concat(talent).concat("/");
    	File imageFilePath = new File(imageFilePathStr);
    	if (!imageFilePath.exists()) {
    		imageFilePath.mkdirs();
    	}//DB登録用カバーイメージ名
    	String coverImage = "digest_".concat(basicFileName);
    	
    	FileBean headerVgaThum = dBean.getHeaderVgaThum();
    	FileBean headerQVgaThrm = dBean.getHeaderQVgaThum();
    	FileBean headerVgaDetail = dBean.getHeaderVgaDetail();
    	FileBean headerQVgaDetail = dBean.getHeaderQVgaDetail();
    	FileBean headerVgaOriginal = dBean.getHeaderVgaOriginal();
    	FileBean headerQVgaOriginal = dBean.getHeaderQVgaOriginal();
    	FileBean headerVgaOriginal2 = dBean.getHeaderVgaOriginal2();
    	FileBean headerQVgaOriginal2 = dBean.getHeaderQVgaOriginal2();
    	if (headerVgaThum != null) {
    		File newFile = new File(imageFilePathStr + "VT_" 
    				+ coverImage.concat(getExtensionFromFileName(headerVgaThum.getFileFileName())));
	    	CommUtil.copyFile(headerVgaThum.getFile(), newFile, true);
    	}
    	if (headerQVgaThrm != null) {
    		File newFile = new File(imageFilePathStr + "QT_" 
    				+ coverImage.concat(getExtensionFromFileName(headerQVgaThrm.getFileFileName())));
	    	CommUtil.copyFile(headerQVgaThrm.getFile(), newFile, true);
    	}
		if (headerVgaDetail != null) {
			File newFile = new File(imageFilePathStr + "VD_" 
					+ coverImage.concat(getExtensionFromFileName(headerVgaDetail.getFileFileName())));
	    	CommUtil.copyFile(headerVgaDetail.getFile(), newFile, true);
    	}
		if (headerQVgaDetail != null) {
			File newFile = new File(imageFilePathStr + "QD_" 
					+ coverImage.concat(getExtensionFromFileName(headerQVgaDetail.getFileFileName())));
	    	CommUtil.copyFile(headerQVgaDetail.getFile(), newFile, true);
    	}
		if (headerVgaOriginal != null) {
			File newFile = new File(imageFilePathStr + "VO_" 
					+ coverImage.concat(getExtensionFromFileName(headerVgaOriginal.getFileFileName())));
	    	CommUtil.copyFile(headerVgaOriginal.getFile(), newFile, true);
    	}
		if (headerQVgaOriginal != null) {
			File newFile = new File(imageFilePathStr + "QO_" 
					+ coverImage.concat(getExtensionFromFileName(headerQVgaOriginal.getFileFileName())));
	    	CommUtil.copyFile(headerQVgaOriginal.getFile(), newFile, true);
    	}
		if (headerVgaOriginal2 != null) {
			File newFile = new File(imageFilePathStr + "VO2_" 
					+ coverImage.concat(getExtensionFromFileName(headerVgaOriginal2.getFileFileName())));
	    	CommUtil.copyFile(headerVgaOriginal2.getFile(), newFile, true);
    	}
		if (headerQVgaOriginal2 != null) {
			File newFile = new File(imageFilePathStr + "QO2_" 
					+ coverImage.concat(getExtensionFromFileName(headerQVgaOriginal2.getFileFileName())));
	    	CommUtil.copyFile(headerQVgaOriginal2.getFile(), newFile, true);
    	}
    	if (headerVgaThum != null && headerQVgaThrm != null
    			&& headerVgaDetail != null && headerQVgaDetail != null
    			&& headerVgaOriginal != null && headerQVgaOriginal != null
    			&& headerVgaOriginal2 != null && headerQVgaOriginal2 != null) {
    		return coverImage.concat(getExtensionFromFileName(headerVgaThum.getFileFileName()));
    	}
    	return null;
    }
    
    public List<DigestContentImageDto> getUploadedFileList() {
    	Map session = ActionContext.getContext().getSession();
    	List<DigestContentImageDto> dContents = (List<DigestContentImageDto>) session.get("contents");
    	session.remove("contents");
    	return dContents;
    }
    public HashMap getContentPoint() throws SQLException {
    	return dao.getContentsPoint();
    }
    public List<CommonMstBean> getKijiDiscValue() throws SQLException {
		return dao.getgetKijiDiscValue();
	}
}
